-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Avoid SeqCst or static mut in mach_timebase_info and QueryPerformanceFrequency caches #77727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Ah, apparently the same pattern exists on windows too: https://github.com/rust-lang/rust/blob/master/library/std/src/sys/windows/time.rs#L199-L221 LARGE_INTEGER is 64 bits, I don't know if we can rely on AtomicU64 on all windows platforms (nor am I confident that relaxed AtomicU64 ops would be a net win on them where it is), but using weaker |
The two places that call this |
Yeah good point. I had also not thought about the fact that that this lets us use Relaxed ops. The cached value on Windows is also nonzero too (it explicitly can't return 0 on any system >= XP, and the current code would panic if it did). So I did this for both. Thanks @m-ou-se. The remaining concern then I guess is for targets that run ios, macos, or windows, but don't support 64-bit atomics. Which according |
At the moment all platforms that mac and windows support will support @bors r+ |
📌 Commit 4f37220 has been approved by |
⌛ Testing commit 4f37220 with merge 4a89ee5cab4196d6de65d43896fa19a6fce1a4b6... |
💔 Test failed - checks-actions |
This was a spurious error:
|
@bors retry |
☀️ Test successful - checks-actions, checks-azure |
This patch went through a couple iterations but the end result is replacing a pattern where an
AtomicUsize
(updated with many SeqCst ops) guards astatic mut
with a singleAtomicU64
that is known to use 0 as a value indicating that it is not initialized.The code in both places exists to cache values used in the conversion of Instants to Durations on macOS, iOS, and Windows.
I have no numbers to prove that this improves performance (It seems a little futile to benchmark something like this), but it's much simpler, safer, and in practice we'd expect it to be faster everywhere where Relaxed operations on AtomicU64 are cheaper than SeqCst operations on AtomicUsize, which is a lot of places.
Anyway, it also removes a bunch of unsafe code and greatly simplifies the logic, so IMO that alone would be worth it unless it was a regression.
If you want to take a look at the assembly output though, see https://godbolt.org/z/rbr6vn for x86_64, https://godbolt.org/z/cqcbqv for aarch64 (Note that this just the output of the mac side, but i'd expect the windows part to be the same and don't feel like doing another godbolt for it). There are several versions of this function in the godbolt:
info_new
: version in the current patchinfo_less_new
: version in initial PRinfo_original
: version currently in the treeinfo_orig_but_better_orderings
: a version that just tries to change the original code's orderings from SeqCst to the (probably) minimal orderings required for soundness/correctness.The biggest concern I have here is if we can use AtomicU64, or if there are targets that dont have it that this code supports. AFAICT: no. (If that changes in the future, it's easy enough to do something different for them)
r? @Amanieu because he caught a couple issues last time I tried to do a patch reducing orderings 😅
I rewrote this whole message so the original is inside here
I happened to notice the code we use for caching the result of mach_timebase_info uses SeqCst exclusively.
However, thinking a little more, it's actually pretty easy to avoid the static mut by packing the timebase info into an AtomicU64.
This entirely avoids needing to do the compare_exchange. The AtomicU64 can be read/written using Relaxed ops, which on current macos/ios platforms (x86_64/aarch64) have no overhead compared to direct loads/stores. This simplifies the code and makes it a lot safer too.
I have no numbers to prove that this improves performance (It seems a little futile to benchmark something like this), although it should do that on both targets it applies to.
That said, it also removes a bunch of unsafe code and simplifies the logic (arguably at least — there are only two states now, initialized or not), so I think it's a net win even without concrete numbers.
If you want to take a look at the assembly output though, see below. It has the new version, the original, and a version of the original with lower Orderings (which is still worse than the version in this PR)
godbolt.org/z/obfqf9 x86_64-apple-darwin
godbolt.org/z/Wz5cWc aarch64-unknown-linux-gnu (godbolt can't do aarch64-apple-ios but that doesn't matter here)
A different (and more efficient) option than this would be to just use the AtomicU64 and use the knowledge that after initialization the denominator should be nonzero... That felt like it's relying on too many things I'm not confident in, so I didn't want to do that.